package cn.lyw.init;

import cn.common.init.InitDataConstant;
import cn.common.redis.RedisServie;
import cn.lyw.discount.service.bo.GetAdminDiscountByStateBO;
import cn.lyw.discount.service.domain.GetAdminDiscountByStateDO;
import cn.lyw.discount.service.service.AdminDiscountService;
import cn.lyw.discount.service.service.UserDiscountService;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.Date;
import java.util.List;

/**
 * @Author lyw
 * @Description 优惠券初始化
 * @Date 10:55 2021/7/20
 * @Param
 * @return
 */
@Configuration
public class DiscountInit implements InitializingBean {

    Logger logger = LoggerFactory.getLogger(DiscountInit.class);

    @Autowired
    private UserDiscountService userDiscountService;

    @Autowired
    private AdminDiscountService adminDiscountService;

    @Autowired
    private RedisServie redisServie;

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("初始化开始");
        initDiscount();
        System.out.println("初始化结束");
    }

    /**
     * @return
     * @Author lyw
     * @Description 初始化活动需要的优惠券
     * @Date 10:58 2021/7/20
     * @Param
     */
    public void initDiscount() {

        //查询未开始发放的优惠券
        GetAdminDiscountByStateBO bo1 = new GetAdminDiscountByStateBO();
        bo1.setDiscountState("notStart");
        List<GetAdminDiscountByStateDO> discount1 = adminDiscountService.getAdminDiscountByState(bo1);

        //将数据存到redis中，并给上时间（时间=优惠券开始时间-初始化时间）
        for (GetAdminDiscountByStateDO discountNotStart : discount1) {
            //获取时间
            long time1 = discountNotStart.getDiscountStart().getTime();
            //当前时间
            long time2 = (new Date()).getTime();
            long l = (time1 - time2) / 1000;

            //存入redis
            redisServie.putHash(InitDataConstant.DISCOUNT_NOT_START + discountNotStart.getDiscountId(), discountNotStart.getDiscountId(), discountNotStart, l);

        }


        //查询活动中的优惠券
        GetAdminDiscountByStateBO bo2 = new GetAdminDiscountByStateBO();
        bo2.setDiscountState("activity");
        List<GetAdminDiscountByStateDO> discount2 = adminDiscountService.getAdminDiscountByState(bo2);

        for (GetAdminDiscountByStateDO discountActivity : discount2) {
            //获取时间
            long time1 = discountActivity.getDiscountEnd().getTime();
            //当前时间
            long time2 = (new Date()).getTime();
            long l = (time1 - time2) / 1000;

            //存入redis
            redisServie.putHash(InitDataConstant.DISCOUNT_ACTIVITY + discountActivity.getDiscountId(), discountActivity.getDiscountId(), discountActivity, l);
            //将它的key存入key表中
            redisServie.putHash(InitDataConstant.DISCOUNT_ACTIVITY_KEY, discountActivity.getDiscountId(), discountActivity);
        }
    }


}
